<?php

class Application_Model_Visitas extends Sgit_Model
{  
    protected $_name = 'visitas';
    protected $_primary = 'id';
    protected $_profiler = FALSE;
   
    
    function myreplace($controller,$action,$idZona,$uri){
        
        $id_user=$this->_auth->getIdentity()->id;
        
        $idZona=($idZona) ? $idZona : 0;
        
        $action_no_registradas=array('insertar','editar','error','tiempo','borrar');
        $controller_no_registradas=array('index','login','error','grafico','rss');
        
   
        if(in_array($controller,$controller_no_registradas)) return true;
        if(in_array($action,$action_no_registradas)) return true;
        
        
        $sql="INSERT INTO visitas (controller,action,idZona,id_user,created_on,uri) VALUES ('$controller','$action',$idZona,$id_user,NOW(),'$uri') ON DUPLICATE KEY UPDATE  n = n+1,updated_on=NOW()";
        
        $this->getAdapter()->query($sql);
        

    }
    
    
    function favoritos($id_user=null,$order=null,$limit=10){
        
        $id_user= (is_null($id_user))? $this->_auth->getIdentity()->id : $id_user;
        
        $order=$order ? "IF(`updated_on`,`updated_on`,`created_on`) DESC": 'n DESC';
        
        $rows=$this->getAll(array('id_user=?'=>$id_user),$order,null,$limit);
        $j=0;
        foreach($rows as $v)
        {
            if($v['idZona']>0) $salida[$j]['id']=$v['controller'].':'.$v['action'].':'.$v['idZona'];
            else $salida[$j]['id']=$v['controller'].':'.$v['action'];
            
            $salida[$j]['uri']=$v['uri'];
            $salida[$j]['controller']=$v['controller'];
            $salida[$j]['action']=$v['action'];
            $salida[$j]['idZona']=$v['idZona'];
            $salida[$j]['updated_on']=$v['updated_on'];
            $salida[$j]['created_on']=$v['created_on'];
            
            $j++;
        }
        
        return $salida;
    }
    
    
    
}  




